/**
 *  Copyright (c) 2017-2022 Rushang Co.Ltd All Rights Reserved.
 */
package com.rshang.rsdp.util.beetl.func;

import javax.servlet.http.HttpServletRequest;

import org.beetl.core.Context;
import org.beetl.core.Function;
import org.beetl.core.exception.BeetlException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.support.RequestContext;

/**
 * 功能简介：自定义spring国际化函数<br/>
 * @author zhangjun
 * @create 2017年7月20日上午11:05:42
 * 
 */
public class I18nFunction implements Function {

	private static final Logger logger = LoggerFactory.getLogger(I18nFunction.class);
	
	/* (non-Javadoc)
	 * @see org.beetl.core.Function#call(java.lang.Object[], org.beetl.core.Context)
	 */
	@Override
	public Object call(Object[] obj, Context context) {
		if(obj.length < 1) {
            //try {
                throw new BeetlException("调用Beetl方法获取国际化信息，参数列表传递错误!");
            /*} catch (BeetlException e) {
                e.printStackTrace();
                return "调用Beetl方法获取国际化信息时 参数列表传递错误";
            }*/
        }
        HttpServletRequest request = (HttpServletRequest) context.getGlobal("request");
        RequestContext requestContext = new RequestContext(request);
        String message = "";
        Object[] param = null;
        try {
        		if(obj.length==1)
        		{
        			message = requestContext.getMessage((String)obj[0]);
        		}
        		else
        		{
        			param = new Object[obj.length-1];
        			System.arraycopy(obj, 1, param, 0, obj.length-1);
        			message = requestContext.getMessage((String)obj[0],param);
        		}
        } catch (Exception e) {
            logger.error("I18nFunction error",e);
        }
        return message;
	}

}
